package com.easy.common.mq;

import com.easy.common.core.domain.entity.EmailBody;
import com.easy.common.utils.ip.AddressUtils;
import com.easy.common.utils.mail.SendMailUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 消息消费者
 *
 * @author RuoYu
 * @date 2023/02/07
 */
@Component
@RocketMQMessageListener(topic = "email", consumerGroup = "group_mail")
public class MessageListener implements RocketMQListener<EmailBody> {
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    @Autowired
    private SendMailUtils sendMailUtils;

    @Override
    public void onMessage(EmailBody emailBody) {
        try {
            long l = System.currentTimeMillis();
            if (emailBody.getType().equals("0")) {
                sendMailUtils.sendMail(emailBody.getSource(), emailBody.getData().toString());
            } else if (emailBody.getType().equals("1")) {
                sendMailUtils.sendErrorMail(emailBody);
            }
            long l2 = System.currentTimeMillis();
            log.info("已完成短信发送业务耗时：" + (l2 - l) / 1000 + "s，----" + emailBody);
        } catch (Exception e) {
            System.out.println("错误");
        }
    }
}
